#include "waypoint.h"
using namespace FsxSimConnect;

//
// JSON representation
//
QString Waypoint::toJson(const int level) const {
    const QString ind = JsonMarkupHelper::Indention(level);
    QString a = ind + "{\n";
    a.append(ind + "\"timestamp\": " + QString::number(Waypoint::_timestampUtcEpoch) + ",\n");
    a.append(ind + "\"time\": \"" + Waypoint::getFormattedTimeLocal() + "\",\n");
    a.append(ind + "\"latitudeDeg\": " + QString::number(Waypoint::getLatitudeDeg()) + ",\n");
    a.append(ind + "\"longitudeDeg\": " + QString::number(Waypoint::getLongitudeDeg()) + ",\n");
    a.append(ind + "\"altitudeFt\": " + QString::number(Waypoint::_altFt) + ",\n");
    a.append(ind + "\"headingTrueDeg\": " + QString::number(Waypoint::getHeadingTrueDeg()) + ",\n");
    a.append(ind + "\"bankDeg\": " + QString::number(Waypoint::getBankDeg()) + ",\n");
    a.append(ind + "\"pitchDeg\": " + QString::number(Waypoint::getPitchDeg()) + "\n");
    a.append(ind + "}\n");
    return a;
}

//
// KML representation
//
QString Waypoint::toKmlPlacemarks(const int level, const bool extPlacemark, const bool modelPlacemark, const QString &modelUrl, const QString &title, const QString &furtherInfo) const {
    QString a = extPlacemark ? Waypoint::toKmlPlacemarkExtended(level, title, furtherInfo) : "";
    if (modelPlacemark) a.append(Waypoint::toKmlPlacemarkWithModel(level, modelUrl, title, furtherInfo));
    return a;
}

//
// KML representation
//
QString Waypoint::toKmlLookAt(const int level) const {
    return JsonMarkupHelper::kmlLookAt(level, Waypoint::getLatitudeDeg(), Waypoint::getLongitudeDeg(), Waypoint::getAltitudeM(),
                                       Waypoint::getHeadingTrueDeg(), Waypoint::getPitchDeg(),Waypoint::getBankDeg());
}

//
// KML representation
//
QString Waypoint::toKmlPlacemarkExtended(const int level, const QString &title, const QString &furtherInfo) const {
    const QString ind = JsonMarkupHelper::Indention(level);
    const QString ind1 = JsonMarkupHelper::Indention(level + 1);
    const QString ind2 = JsonMarkupHelper::Indention(level + 2);
    const QString n = title.isEmpty() ? Waypoint::getFormattedTimeLocal() : title + "@" + Waypoint::getFormattedTimeLocal();

    QString a = ind + "<Placemark>\n";
    a.append(ind1 + "<name>" + n + "</name>\n");
    if (!furtherInfo.isEmpty()) a.append(ind1 + "<name>" + furtherInfo + "</name>\n");
    a.append(ind1 + "<Point>\n");
    a.append(ind2 + JsonMarkupHelper::KmlAltModeAbsolute + "\n");
    a.append(JsonMarkupHelper::kmlCoordinates(level+2, Waypoint::getLatitudeDeg(), Waypoint::getLongitudeDeg(), Waypoint::getAltitudeM()));
    a.append(ind1 + "</Point>\n");
    a.append(ind1 + "<styleUrl>#waypointExtended</styleUrl>\n");
    a.append(ind1 + "<ExtendedData>\n");
    a.append(ind2 + "<Data name=\"timestamp\"><value>" + QString::number(Waypoint::_timestampUtcEpoch) + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"time\"><value>" + Waypoint::getFormattedTimeLocal() + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"latitudeDeg\"><value>" + QString::number(Waypoint::getLatitudeDeg()) + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"longitudeDeg\"><value>" + QString::number(Waypoint::getLongitudeDeg()) + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"altitudeFt\"><value>" + QString::number(Waypoint::getAltitudeFt()) + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"altitudeM\"><value>" + QString::number(Waypoint::getAltitudeM()) + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"headingTrueDeg\"><value>" + QString::number(Waypoint::getHeadingTrueDeg()) + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"bankDeg\"><value>" + QString::number(Waypoint::getBankDeg()) + "</value></Data>\n");
    a.append(ind2 + "<Data name=\"pitchDeg\"><value>" + QString::number(Waypoint::getPitchDeg()) + "</value></Data>\n");
    a.append(ind1 + "</ExtendedData>\n");
    a.append(ind + "</Placemark>\n");
    return a;
}

//
// KML representation
//
QString Waypoint::toKmlPlacemarkWithModel(const int level, const QString &modelUrl, const QString &title, const QString &furtherInfo) const {
    if (modelUrl.isEmpty()) return "";
    const QString ind = JsonMarkupHelper::Indention(level);
    const QString ind1 = JsonMarkupHelper::Indention(level + 1);
    const QString n = title.isEmpty() ? Waypoint::getFormattedTimeLocal() : title + "@" + Waypoint::getFormattedTimeLocal();
    QString a = ind + "<Placemark>\n";
    a.append(ind1 + "<name>" + n + "</name>\n");
    if (!furtherInfo.isEmpty()) a.append(ind1 + "<description>" + furtherInfo + "</description>\n");
    a.append(Waypoint::toKmlModel(level+1, modelUrl));
    a.append(ind + "</Placemark>\n");
    return a;
}

//
// KML representation
//
QString Waypoint::toKmlModel(const int level, const QString &modelUrl) const {
    const QString ind = JsonMarkupHelper::Indention(level);
    const QString ind1 = JsonMarkupHelper::Indention(level + 1);

    QString a = ind + "<Model>\n";
    a.append(ind1 + "<Link><href>" + modelUrl + "</href></Link>\n");
    a.append(ind1 + JsonMarkupHelper::KmlAltModeAbsolute + "\n");
    a.append(JsonMarkupHelper::kmlLocation(level+1, Waypoint::getLatitudeDeg(), Waypoint::getLongitudeDeg(), Waypoint::getAltitudeM()));
    a.append(JsonMarkupHelper::kmlScale(level+1));
    a.append(JsonMarkupHelper::kmlOrientation(level+1, Waypoint::getHeadingTrueDeg(), Waypoint::getBankDeg(), Waypoint::getTiltDeg()));
    a.append(ind + "</Model>\n");
    return a;
}
